package com.sysu.minsysu.weibo.activity;

import com.sysu.R;
import com.sysu.minsysu.weibo.data.OAuth;
import com.sysu.minsysu.weibo.utils.ConfigUtil;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
//import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;



public class AuthorizationAct extends Activity {
	
	private final String LOGTAG = "AuthorizationAct";
	private String category = null;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
	    // TODO Auto-generated method stub
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.authorization_ui);
	    Bundle bundle = new Bundle();
	    bundle = this.getIntent().getExtras();
	    category = bundle.getString("category");
	    
	    //init
	    OAuth  oAuth = OAuth.getInstance();
	    oAuth.clear();
	    	
	    String appKey = ConfigUtil.getInstance().getAppKey();
	    String appSecret = ConfigUtil.getInstance().getAppSecret();
	    oAuth.setKeyAndSecret(appKey, appSecret);
	    
	    String url = oAuth.getAuthorizUrl();
	    Log.d(LOGTAG, "onCreat() [Authoriz] url = "+url);
	   
	    initWebView(url);
	    
    }
	
	private void initWebView(String url) {
		WebView authorizationView = (WebView) findViewById(R.id.authorizationView);
	    authorizationView.clearCache(true);
	    authorizationView.getSettings().setJavaScriptEnabled(true);
	    authorizationView.getSettings().setSupportZoom(true);
	    authorizationView.getSettings().setBuiltInZoomControls(true);
	    authorizationView.setWebViewClient(new WebViewC()); 
	    authorizationView.loadUrl(url);
    }

	class WebViewC extends WebViewClient{
		private int index = 0;
		@Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

			view.loadUrl(url);
			return true;
        }
		
		public void onReceivedSslError (WebView view, SslErrorHandler handler, SslErrorHandler error) {
			 handler.proceed() ;
		 }

		@Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
	        super.onPageStarted(view, url, favicon);
	        Log.d(LOGTAG, "onPageStarted url = " + url );	        
	        
	        if( url.contains(ConfigUtil.callBackUrl) && index == 0){
	        	Log.e(LOGTAG, category );
	        	if (category.equals("xinlang")) {
		        	index ++;
	            	Intent intent = new Intent(AuthorizationAct.this,ShowAccessTokenAct.class);
	            	Bundle bundle = new Bundle();
	    			bundle.putString("category", "xinlang");
	    			bundle.putString(ConfigUtil.OAUTH_VERIFIER_URL, url);
	            	intent.putExtras(bundle);
	            	AuthorizationAct.this.startActivity(intent);
	//	        	savesharedPreferences();
	            	AuthorizationAct.this.finish();
	        	} else if (category.equals("tengxun")) {
	        		index ++;
	            	Intent intent = new Intent(AuthorizationAct.this,ShowAccessTokenAct.class);
	            	Bundle bundle = new Bundle();
	    			bundle.putString("category", "tengxun");
	    			bundle.putString(ConfigUtil.OAUTH_VERIFIER_URL, url);
	            	intent.putExtras(bundle);
	            	AuthorizationAct.this.startActivity(intent);
	//	        	savesharedPreferences();
	            	AuthorizationAct.this.finish();
	        	}
           }
        }

		@Override
        public void onPageFinished(WebView view, String url) {
	        // TODO Auto-generated method stub
	        super.onPageFinished(view, url);
        }
		
	}
	public static final int MODE = MODE_PRIVATE;
	public String PREFERENCE_NAME = "xinlang";
	
//	private void savesharedPreferences(){
//		if (category.equals("xinlang")) {
//			Log.e("authorization success!", category);
//	    	SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCE_NAME, MODE);
//	    	
//	    	String url = getIntent().getStringExtra(ConfigUtil.OAUTH_VERIFIER_URL);
//	    	Uri uri = Uri.parse(url);
//	    	//匹配验证码
//			String oauth_verifier = uri.getQueryParameter("oauth_verifier");				
//	    	OAuth oAuth = OAuth.getInstance();
//			oAuth.setOauthVerifier(oauth_verifier);
//			String accessToken = oAuth.getAccessToken();
//			oAuth.setTokenAndSecret(accessToken);
//			String token = oAuth.getToken();
//			String secret = oAuth.getSecret();
//	        
//			SharedPreferences.Editor editor = sharedPreferences.edit();
//			editor.putString("category", category);
//			editor.putString("oauth_token", token);
//			editor.putString("oauth_token_secret", secret);
//			Log.e(PREFERENCE_NAME, token);
//			Log.e(PREFERENCE_NAME, secret);
//			editor.commit();
//		}
//	}
}
